<?php
// $Id$

/**
 * Controller_Newstype 控制器
 */
class Controller_Newstype extends Controller_Abstract
{
	function __construct($app){
		//得到当前学期信息
		//parent::__construct($app);
		parent::__construct($app);
		//得到该用户可操作的新闻类型
		$user = $this->_app->currentUser();
		$news_op = array();
		if(isset($user['news_op'])){
			if(!empty($user['news_op'])){
				$news_op = explode(',',$user['news_op']);
			    //array_push($search_list_temp," type_id in (".$user['news_op'].")");	
			}
		}
		$this->_view['news_op'] = $news_op;
		//-----------------------------------------------
		$this->_view['type_flag'] = Q::ini('appini/type_flag'); 
	}
	function actionIndex(){
        // 为 $this->_view 指定的值将会传递数据到视图中
		$newstype = new Newstype();
       	$list = $newstype->get_all_list();
       	//unset($list[0]);
       	$myData = array();
       	foreach($list as $k => $v){
       		$can = TRUE;
	    	if (count(Newstype::find('pid=? and isdelete=0',$k)->getAll())) $can = false;
	    	if (count(News::find('type_id=?  and isdelete=0',$k)->getAll())) $can = false;
	    	$myData[$k]['value'] = $v;
	    	$myData[$k]['can_delete'] = $can;
		}
		$this->_view['list'] = $myData;
		$this->_view['subject'] = "信息类型管理";
	}
	function actionCreate(){
		//$newsty = new Newstype();
        //print_r($newsty->get_select_list());
		$form = new Form_Newstype('');
		//var_dump($form);
		if ($this->_context->isPOST() && $form->validate($_POST)){
			$newstype = new Newstype($form->values());
			//echo $task->week_hour;
			$args = Newstype::find('type_name=?&&pid=?&&isdelete=0',$newstype->type_name,$newstype->pid)->getAll();
			$num = count($args);
			if ($num>0) {
				$form['type_name']->invalidate("该类型已经存在");
			} else{
				$newstype->save();
				// 	创建成功后，重定向浏览器
				return $this->_redirect(url(''));
			}
			//$task->save();
			//return $this->_redirect(url(''));
		}
		$this->_view['form'] = $form;
		$this->_view['subject'] = "信息类型管理";
	}
	function actionEdit(){
		// 查询指定 ID
		$id = $this->_context->id;
		$newstype = Newstype::find('id=?', $id)->query();
	 	
		// 构造表单对象
		$form = new Form_Newstype('');
		// 修改表单标题
		$this->_view['subject'] = "信息类型管理";
	    $form->_subject = "修改信息类型";
		if ($this->_context->isPOST() && $form->validate($_POST))
		{
			$tr = Newstype::find('isdelete=0 and type_name=? and id!=? and pid=?',$form['type_name']->value,$id,$form['pid']->value)->getOne();
			if (!empty($tr['type_name'])){
				$form['type_name']->invalidate('该信息类型已存在');
			}elseif($form['pid']->value == $id){
				$form['type_name']->invalidate('对不起，不能选择自己为自己的上级目录！');
			}else{
				//$log_rec = Helper_Util::toArray($newstype);
				// changeProps() 方法可以批量修改对象的属性，但不会修改只读属性的值
				$newstype->changeProps($form->values());
		 
				// 保存并重定向浏览器
				$newstype->save();
				//Log::addlog(1, 'Newstype', $newstype->id(), $log_rec, '修改信息类型：'.$newstype->type_name, NULL, 'newstype');
			// 	创建成功后，重定向浏览器
			return $this->_redirect(url(''));
			}
			
		}
		elseif (!$this->_context->isPOST())
		{
			// 如果不是 POST 提交，则把对象值导入表单
			$form->import($newstype);
		}
	 
		$this->_view['form'] = $form;
		// 重用 create 动作的视图
		$this->_viewname = 'create';
	}
	function actionDelete(){
		// destroyWhere() 方法的参数和 find() 方法完全一致
		$newstype = Newstype::find('id = ?', $this->_context->id)->query();
	 	$newstype->isdelete = 1;
		//$log_rec = Helper_Util::toArray($newstype);
	 	$newstype->save();
		//Log::addlog(2, 'Newstype', $newstype->id(), $log_rec, '删除信息类型：'.$newstype->name, NULL, 'newstype');
		return $this->_redirect(url(''));
	}
}


